package com.examsys.controller;

import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.examsys.po.Admin;
import com.examsys.po.Question;
import com.examsys.po.QuestionDb;
import com.examsys.service.QuestionDbService;
import com.examsys.service.QuestionService;
import com.examsys.util.JsonUtils;
import com.examsys.util.Page;
/**
 * 题目控制层
 *
 */
@WebServlet("/question")
public class QuestionController extends BaseController{
	
	//log4j日志对象
	private Logger log=Logger.getLogger(this.getClass());
	
	private QuestionService questionService;
	
	private QuestionDbService questionDbService;
	
	/**
	 * 初始化工作，这里主要是给业务逻辑层属性做初始化
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		questionService=lookUp("questionService");
		questionDbService=lookUp("questionDbService");
	}
	
	/**
	 * 获取题库下拉框数据
	 * @return
	 * @throws IOException 
	 */
	public void questionDbDatas(HttpServletRequest request, HttpServletResponse response) throws IOException{
		log.info("获取题库下拉框数据");
		List<QuestionDb> questionDbList=null;
		try {
			questionDbList=questionDbService.getList();
		} catch (Exception e) {
			log.error("获取题库下拉框数据失败",e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(questionDbList));
	}
	
	/**
	 * 保存添加
	 * @param question
	 * @return
	 * @throws IOException 
	 */
	public void addSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String dbid = request.getParameter("db_id");
		
		Integer db_id=null;
		
		if(dbid!=null&&!"".equals(dbid)){
			db_id=Integer.valueOf(dbid);
		}
		Question question=new Question();
		log.info("接收到页面添加的数据："+question);//把question放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		
		try {
			Admin admin = (Admin)request.getSession().getAttribute("ADMIN");
			
			QuestionDb questionDb = questionDbService.get(db_id);
			question.setAdmin(admin);
			question.setQuestionDb(questionDb);
			question.setCreate_date(new Date(System.currentTimeMillis()));
			
			boolean flag = questionService.add(question);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			log.error("添加失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 初始化修改
	 * @param id
	 * @return
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id = Integer.valueOf(request.getParameter("id"));
		log.info("开始初始化编号为"+id+"的题目数据供前端修改");//把id放到日志
		Question question=null;
		try {
			question=questionService.get(id);
		} catch (Exception e) {
			log.error("初始化修改失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(question));
	}
	
	/**
	 * 保存修改
	 * @param question
	 * @param db_id
	 * @return
	 * @throws IOException 
	 */
	public void updateSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String dbid = request.getParameter("db_id");
		
		Integer db_id=null;
		
		if(dbid!=null&&!"".equals(dbid)){
			db_id=Integer.valueOf(dbid);
		}
		
		Question question=new Question();
		log.info("接收到页面修改的数据："+question+",题库编号："+db_id);//把question放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		
		try {
			Question oldquestion=questionService.get(question.getId());
		
			oldquestion.setQtype(question.getQtype());
			oldquestion.setQlevel(question.getQlevel());
			oldquestion.setQfrom(question.getQfrom());
			oldquestion.setContent(question.getContent());
			oldquestion.setSkey(question.getSkey());
			oldquestion.setKey_desc(question.getKey_desc());
			oldquestion.setCreate_date(question.getCreate_date());
			oldquestion.setStatus(question.getStatus());
			oldquestion.setRemark(question.getRemark());
			oldquestion.setNotInIds(question.getNotInIds());
			
			QuestionDb questionDb = questionDbService.get(db_id);
			question.setQuestionDb(questionDb);
			
			boolean flag = questionService.update(oldquestion);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			log.error("修改失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 删除题目
	 *@param id 待删除的题目的编号
	 * @throws IOException 
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id = Integer.valueOf(request.getParameter("id"));
		log.info("开始删除编号为"+id+"的系统参数");//把id放到日志
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		
		try {
			boolean flag = questionService.delete(id);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			log.error("删除失败", e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 题目管理
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		log.info("进入题目管理页面");
		request.getRequestDispatcher("question/questionManager").forward(request, response);//设置显示页面
		
		
	}
	
	/**
	 * 题目管理表格数据
	 * @param question 查询条件
	 * @param page 页码
	 * @param rows 每一页行数
	 * @return
	 * @throws IOException 
	 */
	public void selectDatas(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		
		Integer ppage=null;
		
		if(page!=null&&!"".equals(page)){
			ppage=Integer.valueOf(page);
		}
		
		Integer rrows=null;
		
		if(rows!=null&&!"".equals(rows)){
			rrows=Integer.valueOf(rows);
		}
		Question question =new Question();
		log.info("开始获取题目管理表格数据");
		Map jsonDatas=new HashMap();//待返回json集合数据;
		try {
			//分页处理
			Page<Question> pageObj = questionService.getList(question,ppage,rrows);
			//取记录总条数
			jsonDatas.put("total", pageObj.getTotal());//总数
			jsonDatas.put("rows", pageObj.getResultlist());//前端需要的行数据
		} catch (Exception e) {
			log.error("获取题目管理表格数据失败",e);
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
}





